package com.txt.call;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.library.control.TxtKandy;
import com.example.library.login.AccessKandy;
/**
 * Created by DELL on 2017/8/18.
 */
public class LoginActivity extends Activity {
    private String mUser="10001@sdkdemo.txtechnology.com.cn";
    private String mPassward="ai123456";
    private final String TAG=LoginActivity.class.getSimpleName();
    private EditText mUserEdit;
    private EditText mPasswardEdit;
    private Button mPhoneRegeister;
    private static final int REQUEST_PHONE=1;
    private static final int REQUEST_USERNAME=2;
    private Button mLoginButton;
    private ProgressDialog mProgreeDialog;
    private Button mRegisterButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }
    private void initView() {
        if (TxtKandy.getAccessKandy().userIsLogin()){
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            LoginActivity.this.finish();
            return;
        }
        if (!TxtKandy.getDataMpvConnnect().getCurrentLoginUser().equals("")&&!TxtKandy.getDataMpvConnnect().getCurrentLoginUserPassward().equals("")){
            userLogin(TxtKandy.getDataMpvConnnect().getCurrentLoginUser(),TxtKandy.getDataMpvConnnect().getCurrentLoginUserPassward());
        }
        mUserEdit= (EditText) findViewById(R.id.edit_user);
        mUserEdit.setText(mUser);
        mPasswardEdit= (EditText) findViewById(R.id.edit_passward);
        mPasswardEdit.setText(mPassward);
        mLoginButton= (Button) findViewById(R.id.loginbutton);
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TxtKandy.getAccessKandy().userIsLogin()){
                    Toast.makeText(LoginActivity.this,"请先退出当前账号，才能重新登录！",Toast.LENGTH_LONG).show();
                    return;
                }
                userLogin(mUserEdit.getText().toString(),mPasswardEdit.getText().toString());
            }
        });
        mPhoneRegeister= (Button) findViewById(R.id.phoneregeister);
        mPhoneRegeister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TxtKandy.getAccessKandy().userIsLogin()){
                    Toast.makeText(LoginActivity.this,"请先退出当前账号，才能重新登录！",Toast.LENGTH_LONG).show();
                    return;
                }
                Intent intent =new Intent(LoginActivity.this,PhoneRegisterActivity.class);
                startActivityForResult(intent,REQUEST_PHONE);
            }
        });
        mRegisterButton= (Button) findViewById(R.id.userregister);
        mRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,UserRegisterActivity.class);
                startActivityForResult(intent,REQUEST_USERNAME);
            }
        });
    }

    private void userLogin(final String user,String passward) {
        mProgreeDialog= ProgressDialog.show(this,null,"正在登录用户，请稍后....",true,false);
        mProgreeDialog.setCancelable(true);
        TxtKandy.getAccessKandy().userLogin(user, passward, new AccessKandy.LoginRequestCallBack() {
            @Override
            public void onSuccess() {
                mProgreeDialog.dismiss();
                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                LoginActivity.this.finish();
            }
            @Override
            public void onFail(int i, String s) {
                LoginActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mProgreeDialog.dismiss();
                        Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (REQUEST_PHONE==requestCode){
                boolean isLogin= data.getBooleanExtra(PhoneRegisterActivity.LOGIN,false);
                if (isLogin){
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    LoginActivity.this.finish();
                }
            }else if (REQUEST_USERNAME==requestCode){
                    String username=data.getStringExtra(UserRegisterActivity.USERNAME);
                    String passward=data.getStringExtra(UserRegisterActivity.PASSWARD);
                    userLogin(username,passward);
                }
            }
        }

}
